package com.sanjiahantang.ythshopping.presenter;

import android.content.Context;
import android.text.TextUtils;

import com.sanjiahantang.ythshopping.config.ConfigValue;
import com.sanjiahantang.ythshopping.http.BaseDelegate;
import com.sanjiahantang.ythshopping.http.ExceptionHelper;
import com.sanjiahantang.ythshopping.http.OkHttpClientManager;
import com.sanjiahantang.ythshopping.model.AreaBean;
import com.sanjiahantang.ythshopping.model.HomePageModel;
import com.sanjiahantang.ythshopping.utils.Utils;
import com.sanjiahantang.ythshopping.views.HomePageView;
import com.squareup.okhttp.Request;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by ZHY_9 on 2017/6/16.
 */

public class HomePagePresenter extends BasePresenter {

    private HomePageView homePageView;

    public HomePagePresenter(HomePageView homePageView) {
        this.homePageView = homePageView;
    }

    public void getHomePageData(final Context context){
        initLoadDialog(context);
        mLoadingDialog.show();
        Map<String,String> params=new HashMap<>();
        String address = ConfigValue.currentCity.getRegion_name();
        if(null != ConfigValue.currentCity.getParent_name() && !TextUtils.isEmpty(ConfigValue.currentCity.getParent_name())){
            address = ConfigValue.currentCity.getParent_name() + "," + address;
        }

        if(address == null){
            address = "北京";
        }
        params.put("region_name",address);
        if(ConfigValue.rid != null){
            params.put("rid",ConfigValue.rid);
        }

        OkHttpClientManager.postAsyn(context, ConfigValue.APP_IP + "first/index",params, new BaseDelegate.ResultCallback<HomePageModel>() {
            @Override
            public void onError(Request request, Object tag, Exception e) {
                dismiss();
                Utils.showToast(context, ExceptionHelper.getMessage(e, context));
            }

            @Override
            public void onResponse(HomePageModel response, Object tag) {
                dismiss();
                homePageView.getData(response);
            }
        });
    }
}
